var mysql = require('mysql');

//创建一个connection
var connection = mysql.createConnection({
	host : 'localhost',
	user : 'root',
	password :'root',
	port : '3306',
	database: 'nodemysql',
});

connection.connect();


//执行sql语句

//增加数据操作
var userAddSql = 'insert into userinfo(Id,UserName,UserPass) values(0,?,?)';
var userAddSql_Params = ['ren','eeee'];
connection.query(userAddSql,userAddSql_Params,function(err,result){
	if (err) {
		console.log('[INSERT ERROR] -',err.message);
		return;
	}

	console.log('****************************INSERT*********************');
	console.log('INSERT ID:',result);
	console.log('*******************************************************\n\n');
});


//改数据操作
var userModSql = 'UPDATE userinfo SET UserName = ?,UserPass = ? where Id= ?';
var userModSql_Params = ['zhizhong','3242','9'];
connection.query(userModSql,userModSql_Params,function(err,result){
	if (err) {
		console.log('[UPDATE ERROR] -',err.message);
		return;
	}

	console.log('****************************UPDATE*********************');
	console.log('UPDATE affectedRows:',result.affectedRows);
	console.log('*******************************************************\n\n');
});

//查数据操作
var userGetSql = 'select * from userinfo';
connection.query(userGetSql,function(err,result){
	if (err) {
		console.log('[SELECT ERROR] - ',err.message);
		return;
	}
	
	console.log('**************************SELECT***********************');
	console.log(result);
	console.log('*******************************************************\n\n');
});

//删数据操作
var userDelSql = 'DELETE FROM userinfo';
connection.query(userDelSql,function(err,result){
	if (err) {
		console.log('[DELETE ERROR] -',err.message);
		return;
	}

	console.log('*************************DELETE*******************');
	console.log('DELETE affectedRows',result.affectedRows);
	console.log('*************************DELETE*******************');
});



//关闭connection
connection.end(function(err){
	if (err) {
		return;
	}
	console.log('[connection end] succeed!');
});
